import requests
import urllib3
from configs.settings import API_URL
from libraries.logger import log

urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)


def add_goods_to_cart(token, goods_id, dress_type, color_id, size_id, goods_num=1):
    """加车"""
    headers = {"Content-Type": "application/json", "x-token": f"{token}", "x-countryCode": 'US', "x-app": "h5"}
    data = {"goods_id": goods_id, "dress_type": dress_type, "from_showroom": "", "from_whatAreU": "",
            "recommend_flag": "", "from_details_entry": "", "from_instagram": "",
            "styles": {
                "freeStyle": False, "size_type": "_inch",
                "select": {
                    "color": color_id, "size": size_id
                }
            },
            "goods_number": goods_num}
    response = requests.post(API_URL + '1.0/cart/goods', headers=headers, json=data, verify=False)
    log.info(f"通过api加车， status_code：{response.status_code}")
    if response.status_code != 200:
        return add_goods_to_cart(token, goods_id, dress_type, color_id, size_id, goods_num=1)
    return response.json()['data']['rec_id']


def change_goods_as_rush(token, rec_id):
    """加急"""
    headers = {"Content-Type": "application/json", "x-token": f"{token}", "x-countryCode": 'US'}
    data = {"for_checkout": 1, "goods_number": 1, "rush_group_id": 1, "rush_type": "rush"}
    response = requests.post(API_URL + f'1.0/cart/goods/{rec_id}', headers=headers, json=data, verify=False)
    log.info(f"通过api修改商品为rush，status_code：{response.status_code}")
    if response.status_code != 200:
        return change_goods_as_rush(token, rec_id)
